{extend name="common/base" /}
{block name="content"}
    <div class="iframe_div layui-anim layui-anim-upbit">
        <div class="top_btn_tool">
            {if power('plugin_create','上传插件','plugin')}
            <button class="layui-btn" data-type="create_btn" id="create_btn">上传插件</button>
            {/if}
        </div>
        <div class="clear"></div>
        <div class="table_div">
            <table class="layui-hide" id="table_list" lay-filter="table_list"></table>
        </div>
    </div>
{/block}
{block name="script"}
<script>
    layui.config({
        base: '<?php echo SITE;?>static/layuiadmin/'
    }).extend({
        index: 'lib/index'
    }).use(['index'],async () => {
        const { layer,form, admin, table, $,element,upload } = layui;

        /*渲染表格*/
        let table_list = table.render({
            elem: '#table_list'
            ,url:'/admin/plugin/list_data'
            ,page:true
            ,limit:15
            ,cols: [[
                {field:'id', width:80, title: 'ID'}
                ,{field:'title', width:120, title: '插件名称'}
                ,{field:'description', title: '描述'}
                ,{field:'version', width:150, title: '版本'}
                ,{field:'create_time',width:180, title: '安装时间'}
                ,{field:'frozen',width:100, title: '状态', templet: '#switchTpl', unresize: true}
                ,{toolbar: '#barDemo',width:170, title: '操作'}
            ]]
        });

        /*监听表格动作*/
        table.on('tool(table_list)',async function(obj){
            let data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除行么',async function(index){
                    let res = await admin.delete('/admin/plugin/'+data.id);
                    if(res.status){
                        obj.del();
                        layer.close(index);
                        setTimeout(function (){
                            parent.location.reload();
                        },1000)
                    }
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    , title: '编辑'
                    , area: ['600px','600px']
                    , maxmin: true
                    , shade: 0.6
                    , content: ['/admin/plugin/'+data.id]
                });
            } else if(obj.event === 'update'){
                await admin.put('/admin/plugin_update/'+data.id,{},false,table_list);
            }
        });

        /*重载表格*/
        function sync_data(){
            table.reload('table_list', {
                page: {
                    curr: 1
                }
            });
        }

        /*监听状态*/
        form.on('switch(frozenFilter)',async function(obj){
            let frozen = obj.elem.checked?0:1;
            const res = await admin.put('/admin/plugin/'+this.value,{frozen:frozen},false,table_list);
            if(res.status){
                setTimeout(function (){
                    parent.location.reload();
                },1000)
            }
        });

        /*监听按钮动作*/
        $('.top_btn_tool .layui-btn').on('click', function(){
            let type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        let active = {
            create_btn:function (){

            },
        }

        /*上传*/

        let uploadInst = upload.render({
            elem: '#create_btn'
            ,url: '/admin/plugin_update'
            ,field:'plugin'
            ,accept:'file'
            ,done: function(res){
                layer.msg(res.msg);
                if(res.status){
                    setTimeout(function (){
                        parent.location.reload();
                    },1000)
                    return false;
                }
            }
            ,error: function(){
                layer.msg('上传异常');
            }
        });


        /*上传end*/


    });
</script>
{/block}


{block name="template"}
{include file="plugin/template" /}
{/block}